package db_svr

import (
	"fmt"

	ora "gopkg.in/rana/ora.v3"
)

//	"encoding/json"

func ModifyUserAlertNum(o chan string, p_user_id int32, p_interface string, p_old_alert_num, p_new_alert_num int64) {

	// logger = log.New(os.Stdout, ">>>>>DB.Modify User Alert Number:", log.LstdFlags|log.Lshortfile)

	var v_flag int8

	var v_ret_msg string

	var v_data string //Lob

	if _, err := ses.PrepAndExe("CALL  pkg_user_info.Modify_user_Alert_num(:p1,:p2 , :p3, :p4, :p5, :p6, :p7 )",
		//		 &v_flag, &v_ret_msg,  &v_data_msg, int32(p_parent_id) ); err != nil {
		&v_flag, &v_ret_msg, &v_data, int32(p_user_id), int64(p_old_alert_num), int64(p_new_alert_num), p_interface); err != nil {

		logger.Panic(err)
	}

	logger.Printf(">>> flag: %v, Msg:%v, Data: %v\n", v_flag, v_ret_msg, v_data)

	o <- v_data

}

func SetDeviceMargin(o chan string, p_user_id int32, p_device_id, p_interface string, p_oxy_min, p_ph_min, p_ph_max, p_ammonia_nitrogen, p_temperature_min float64) {

	var v_flag int8
	var v_ret_msg string
	var v_data_msg string
	var v_created_by string = "TDE"

	// logger = log.New(os.Stdout, ">>>>>>>DB.Set Device Margin:", log.LstdFlags|log.Lshortfile)

	if _, err := ses.PrepAndExe("CALL pkg_device_data.set_Device_Margin(:p1,:p2 , :p3, :p4, :p5, :p6, :p7, :p8, :p9, :p10, :p11 ,:p12 )",
		&v_flag, &v_ret_msg, &v_data_msg, int32(p_user_id), p_device_id, v_created_by,
		p_oxy_min, p_ph_min, p_ph_max, p_ammonia_nitrogen, p_temperature_min, p_interface); err != nil {

		logger.Panicf("***Panic in Set_Device_Margin:%v ***\n", err)
	}

	logger.Println(">>>user_id:", p_user_id, ",flag:", v_flag, ",Msg:", v_ret_msg, "Data:", v_data_msg)

	o <- v_data_msg

}

func UserAuth(uname, verifyMode, pass, ip, valid_dt, exp_dt string, port int32) (int32, Ua_retMsg) {
	// return  value
	//   if  Flag = 0 : succeed
	//     account_id: user_id  from column user_info.user_id in DB
	//       Message : json format of last three login logs within latest 90 days
	//
	//    if Flag  = -1: failed
	//       account_id and Message: useless

	var flag int8 = -1
	var user_id, v_duration int32
	var msg, data string

	logger.Printf("uname:%v, mode:%v, pass:%v, ip:%v, port:%v, valit_dt:%v, exp_dt:%v\n",
		uname, verifyMode, pass, ip, port, valid_dt, exp_dt)

	if _, err := ses.PrepAndExe("CALL pkg_user_auth.tde_user_login(:p1,:p2,:p3,:p4,:p5, :p6, :p7 , :p8 , :p9, :p10, :p11)",
		uname, verifyMode, pass, ip, port, valid_dt, exp_dt, &user_id, &msg, &data, &v_duration); err != nil {

		//		logger.Printf(" Type of port: %T, value of port: %v", port, port)
		logger.Panicf("***Panic in User_Auth: %v ***\n", err)
	}

	logger.Printf("PKG_USER_AUTH.TDE_USER_LOGIN says : [user_id:%v, msg:%v, data : %v]\n", user_id, msg, data)

	if user_id > 0 {
		flag = 0
	}

	return v_duration, Ua_retMsg{Flag: flag,
		Message: msg,
		User_id: int(user_id),
		Data:    data,
	}
}

func DataSync(device_id, start_dt, end_dt string, o chan string) {

	logger.Printf("Data_sync(%v,%v,%v, o)\n", device_id, start_dt, end_dt)
	procRset := &ora.Rset{}

	stmtRefCall, err := ses.Prep("CALL pkg_device_data.Device_Tde_Sync(:p1,:p2, :p3, :p4 )")

	defer stmtRefCall.Close()
	if err != nil {
		logger.Panic(err)
	}

	_, err = stmtRefCall.Exe(procRset, device_id, start_dt, end_dt)
	if err != nil {
		logger.Panic(err)
	}

	if procRset.IsOpen() {
		//        logger.Println( "\n ............. procRset Is open ....... ")
		for procRset.Next() {
			logger.Printf("Fetched : %v\n", procRset.Row[0])
			o <- fmt.Sprintf("%v", procRset.Row[0])
		}
	}

	var rowsAffected int32
	_, err = ses.PrepAndExe("CALL pkg_device_data.Device_Tde_Synced(:p1,:p2,:p3,:p4)", device_id, start_dt, end_dt, &rowsAffected)
	logger.Printf("Updated %v rows status as synced.\n", rowsAffected)

	return

}
